package com.summer.Thread;

import java.util.concurrent.locks.ReentrantLock;

class Window implements Runnable{
    private static int ticket = 100;
    private ReentrantLock lock = new ReentrantLock(true);//true公平锁 否
    @Override
    public void run() {
        while (true){
            try {
                lock.lock();
                if (ticket>0){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+": 售票 ,票号为:" + ticket);
                    ticket--;
                }else break;
            }finally {
                lock.unlock();
            }

        }
    }
}
public class LockTest {
    public static void main(String[] args) {
        Window window = new Window();
        Thread thread1 = new Thread(window,"窗口1");
        Thread thread2 = new Thread(window,"窗口1");
        Thread thread3 = new Thread(window,"窗口1");
        thread1.start();
        thread2.start();
        thread3.start();
    }


}
